﻿using Deployment.Data.UI.Dialogs;
using Deployment.Data.Variable;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Variable
{
    /// <summary>
    /// Interaction logic for VariableEditorControl.xaml
    /// </summary>
    public partial class VariableEditorControl : AbstractDialogContentControl
    {
        public static readonly DependencyProperty EditModeProperty =
            DependencyProperty.Register("EditMode",
                typeof(bool),
                typeof(VariableEditorControl));

        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(VariableEditorControl));

        public static readonly DependencyProperty VariablesProperty =
            DependencyProperty.Register("Variables",
                typeof(Data.Variable.VariablesFile),
                typeof(VariableEditorControl));

        public event DependencyPropertyChangedEventHandler PropertyChanged;

        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }
        

        public VariableEditorControl()
        {
            InitializeComponent();
            DataContext = this;
        }

        public bool EditMode
        {
            get { return (bool)GetValue(EditModeProperty); }
            set
            {
                SetValueDp(EditModeProperty, value);
            }
        }

        protected internal override bool isContentOk()
        {
            if (contentControl.Content != null && contentControl.Content is AbstractVariableEditorControl)
            {
                return (contentControl.Content as AbstractVariableEditorControl).ContentOk;
            }
            return false;
        }

        private Binding contenOkBinding = null;

        private void updateContentDependancyProperties()
        {
            if (contentControl.Content != null && contentControl.Content is AbstractVariableEditorControl)
            {
                (contentControl.Content as AbstractVariableEditorControl).Environments = this.Environments;
                (contentControl.Content as AbstractVariableEditorControl).Variables = this.Variables;
                //(contentControl.Content as AbstractVariableEditorControl).Variable = this.cbo_VariableType.SelectedItem as AbstractVariable;

                //Create a binding to the Content Ok property of the Variable to ContentOk Property of the Content Control.
                if (contenOkBinding != null)
                    BindingOperations.ClearBinding(this, ContentOkProperty);

                contenOkBinding = new Binding();
                contenOkBinding.Source = (contentControl.Content as AbstractVariableEditorControl).Variable;
                contenOkBinding.Path = new PropertyPath("isContentOk");
                contenOkBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                BindingOperations.SetBinding(this, ContentOkProperty, contenOkBinding);

            }
        }

        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set 
            { 
                SetValueDp(EnvironmentsProperty, value);
                updateContentDependancyProperties();
            }
        }

        public Data.Variable.VariablesFile Variables
        {
            get { return (Data.Variable.VariablesFile)GetValue(VariablesProperty); }
            set
            {
                SetValueDp(VariablesProperty, value);
                updateContentDependancyProperties();
            }
        }

        //private void contentControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        //{
        //    Debug.WriteLine(contentControl.DataContext);
        //    updateContentDependancyProperties();
        //}

        public AbstractVariable newVariable
        {
            get 
            {
                if (contentControl.Content != null && contentControl.Content is AbstractVariableEditorControl)
                    return (contentControl.Content as AbstractVariableEditorControl).Variable;
                return null;
            }
        }

        private void contentControl_ContentChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            updateContentDependancyProperties();

        }
    }
}
